package org.cycuchy.hp.components.book.gallery {
	
	import flash.display.DisplayObject;
	
	import mx.collections.ArrayCollection;
	
	import org.cycuchy.hp.components.FotoCanvas;
	import org.cycuchy.hp.components.book.PageWithBrowser;
	import org.cycuchy.hp.data.FotoData;
	import org.cycuchy.hp.data.FotosData;
	import org.cycuchy.hp.data.IDatas;
	import org.cycuchy.hp.event.PreloadingImageEvent;
	
	public class GalleryPage extends PageWithBrowser {
		
		private var cap:String = null;
		
		public function GalleryPage() {
			super();
		}
		
		override public function loadContent(offset:Number):void {
//			trace("GalleryPage::loadContent");
			var child:DisplayObject;
			//FIXME: removeallchildren()
			var children:Array = container.getChildren();
			for each (child in children) {
				if (child is FotoCanvas) {
					(child as FotoCanvas).clean();					
				}
			}
			
			 
			var _data:FotosData = FotosData(_manager.datas);
			var cols:uint = _data.cols;
			var rows:uint = _data.rows;
			//var polaroidWidth:uint = _data.polaroidWidth;
			//var polaroidHeight:uint = _data.polaroidHeight;
			//var scaleX:Number = _data.scaleX;
			//var scaleY:Number = _data.scaleY;
			//var verticalGap:uint = _data.verticalGap;
			//var horizontalGap:uint = _data.horizontalGap;
			var fotos:ArrayCollection = _data.fotos;
			//var fotoC:FotoCanvas;
			var foto:FotoData;
			//var xCenter:uint = width/2 - (cols*polaroidWidth + (cols-1)*horizontalGap)/2;
			//var yCenter:uint = height/2 - (rows*polaroidHeight + (rows-1)*verticalGap)/2;
			var count:uint = offset;
//			var child:DisplayObject;
			var gn:String = null;
			for (var row:uint=0; row<rows; row++) {
				for (var col:uint=0; col<cols; col++) {
					if (fotos.length > count) {
						foto = FotoData(fotos.getItemAt(count));
						if (foto != null) {
							child = container.getChildAt(count - offset);
							if (child is FotoCanvas) {
								(child as FotoCanvas).fotoData = foto;
								gn = foto.galleryName;
//								if (cap != gn) {
//									cap = gn;
//								}
							}
						}
						//fotoC = new FotoCanvas(scaleX, scaleY, polaroidWidth, polaroidHeight, foto);
						//fotoC.x = xCenter + horizontalGap*col + col*polaroidWidth;
						//fotoC.y = yCenter + verticalGap*row + row*polaroidHeight;
						//container.addChild(fotoC);
						count++;
					}
				}
			}
			if (cap != gn) {
				cap = gn;
			}
			
			super.loadContent(offset);
			dispatch(PreloadingImageEvent.LOAD, 0);						
		}
		
		private function dispatch(event:String, index:int):void {
			var children:Array = container.getChildren();
			for each (var child:DisplayObject in children) {
				if (child is FotoCanvas) {
					child.dispatchEvent(new PreloadingImageEvent(event, index));					
				}
			}
		}
		
		override public function init(o:IDatas):void {
			var _data:FotosData = FotosData(o);
			var cols:uint = _data.cols;
			var rows:uint = _data.rows;
			var polaroidWidth:uint = _data.polaroidWidth;
			var polaroidHeight:uint = _data.polaroidHeight;
			var scaleX:Number = _data.scaleX;
			var scaleY:Number = _data.scaleY;
			var verticalGap:uint = _data.verticalGap;
			var horizontalGap:uint = _data.horizontalGap;
			var fotos:ArrayCollection = _data.fotos;
			var fotoC:FotoCanvas;
//			var foto:FotoData;
			var xCenter:uint = container.width/2 - (cols*polaroidWidth + (cols-1)*horizontalGap)/2;
			var yCenter:uint = container.height/2 - (rows*polaroidHeight + (rows-1)*verticalGap)/2;
			var count:uint = 0;
			for (var row:uint=0; row<rows; row++) {
				for (var col:uint=0; col<cols; col++) {
//					if (fotos.length > count) {
						//foto = FotoData(fotos.getItemAt(count));
						fotoC = new FotoCanvas(scaleX, scaleY, polaroidWidth, polaroidHeight);
						fotoC.x = xCenter + horizontalGap*col + col*polaroidWidth;
						fotoC.y = yCenter + verticalGap*row + row*polaroidHeight;
						container.addChild(fotoC);
						count++;
//					}
				}
			}
		}		
		
		override protected function getCaption():String {
			trace("getcaption ", cap);
			return cap; 
		}
		
	}
	
}